<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="接收人id" prop="receiveD">
      <el-input v-model="dataForm.receiveD" placeholder="接收人id"></el-input>
    </el-form-item>
    <el-form-item label="部门（基地）id" prop="deptId">
      <el-input v-model="dataForm.deptId" placeholder="部门（基地）id"></el-input>
    </el-form-item>
    <el-form-item label="设备id" prop="equipmentId">
      <el-input v-model="dataForm.equipmentId" placeholder="设备id"></el-input>
    </el-form-item>
    <el-form-item label="田块id" prop="farmlandInfoId">
      <el-input v-model="dataForm.farmlandInfoId" placeholder="田块id"></el-input>
    </el-form-item>
    <el-form-item label="警告数值" prop="warningValue">
      <el-input v-model="dataForm.warningValue" placeholder="警告数值"></el-input>
    </el-form-item>
    <el-form-item label="警告语" prop="warningText">
      <el-input v-model="dataForm.warningText" placeholder="警告语"></el-input>
    </el-form-item>
    <el-form-item label="警告类型" prop="warningType">
      <el-input v-model="dataForm.warningType" placeholder="警告类型"></el-input>
    </el-form-item>
    <el-form-item label="延时推送时长（小时）" prop="sleepTime">
      <el-input v-model="dataForm.sleepTime" placeholder="延时推送时长（小时）"></el-input>
    </el-form-item>
    <el-form-item label="告警次数" prop="warningNum">
      <el-input v-model="dataForm.warningNum" placeholder="告警次数"></el-input>
    </el-form-item>
    <el-form-item label="预警规则id" prop="warningRoleId">
      <el-input v-model="dataForm.warningRoleId" placeholder="预警规则id"></el-input>
    </el-form-item>
    <el-form-item label="接收人姓名" prop="receiveName">
      <el-input v-model="dataForm.receiveName" placeholder="接收人姓名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          receiveD: '',
          deptId: '',
          equipmentId: '',
          farmlandInfoId: '',
          warningValue: '',
          warningText: '',
          warningType: '',
          sleepTime: '',
          warningNum: '',
          warningRoleId: '',
          receiveName: '',
          createId: '',
          createName: '',
          createTime: '',
          updateId: '',
          updateTime: '',
          updateName: '',
          version: '',
          gcFlag: ''
        },
        dataRule: {
          receiveD: [
            { required: true, message: '接收人id不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '部门（基地）id不能为空', trigger: 'blur' }
          ],
          equipmentId: [
            { required: true, message: '设备id不能为空', trigger: 'blur' }
          ],
          farmlandInfoId: [
            { required: true, message: '田块id不能为空', trigger: 'blur' }
          ],
          warningValue: [
            { required: true, message: '警告数值不能为空', trigger: 'blur' }
          ],
          warningText: [
            { required: true, message: '警告语不能为空', trigger: 'blur' }
          ],
          warningType: [
            { required: true, message: '警告类型不能为空', trigger: 'blur' }
          ],
          sleepTime: [
            { required: true, message: '延时推送时长（小时）不能为空', trigger: 'blur' }
          ],
          warningNum: [
            { required: true, message: '告警次数不能为空', trigger: 'blur' }
          ],
          warningRoleId: [
            { required: true, message: '预警规则id不能为空', trigger: 'blur' }
          ],
          receiveName: [
            { required: true, message: '接收人姓名不能为空', trigger: 'blur' }
          ],
          createId: [
            { required: true, message: '创建人id不能为空', trigger: 'blur' }
          ],
          createName: [
            { required: true, message: '创建用户姓名不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateId: [
            { required: true, message: '修改人id不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          updateName: [
            { required: true, message: '修改用户姓名不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          gcFlag: [
            { required: true, message: '删除标识 -1：删除 0：正常不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtuwarningrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.receiveD = data.bussrtuwarningrecord.receiveD
                this.dataForm.deptId = data.bussrtuwarningrecord.deptId
                this.dataForm.equipmentId = data.bussrtuwarningrecord.equipmentId
                this.dataForm.farmlandInfoId = data.bussrtuwarningrecord.farmlandInfoId
                this.dataForm.warningValue = data.bussrtuwarningrecord.warningValue
                this.dataForm.warningText = data.bussrtuwarningrecord.warningText
                this.dataForm.warningType = data.bussrtuwarningrecord.warningType
                this.dataForm.sleepTime = data.bussrtuwarningrecord.sleepTime
                this.dataForm.warningNum = data.bussrtuwarningrecord.warningNum
                this.dataForm.warningRoleId = data.bussrtuwarningrecord.warningRoleId
                this.dataForm.receiveName = data.bussrtuwarningrecord.receiveName
                this.dataForm.createId = data.bussrtuwarningrecord.createId
                this.dataForm.createName = data.bussrtuwarningrecord.createName
                this.dataForm.createTime = data.bussrtuwarningrecord.createTime
                this.dataForm.updateId = data.bussrtuwarningrecord.updateId
                this.dataForm.updateTime = data.bussrtuwarningrecord.updateTime
                this.dataForm.updateName = data.bussrtuwarningrecord.updateName
                this.dataForm.version = data.bussrtuwarningrecord.version
                this.dataForm.gcFlag = data.bussrtuwarningrecord.gcFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussrtuwarningrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'receiveD': this.dataForm.receiveD,
                'deptId': this.dataForm.deptId,
                'equipmentId': this.dataForm.equipmentId,
                'farmlandInfoId': this.dataForm.farmlandInfoId,
                'warningValue': this.dataForm.warningValue,
                'warningText': this.dataForm.warningText,
                'warningType': this.dataForm.warningType,
                'sleepTime': this.dataForm.sleepTime,
                'warningNum': this.dataForm.warningNum,
                'warningRoleId': this.dataForm.warningRoleId,
                'receiveName': this.dataForm.receiveName,
                'createId': this.dataForm.createId,
                'createName': this.dataForm.createName,
                'createTime': this.dataForm.createTime,
                'updateId': this.dataForm.updateId,
                'updateTime': this.dataForm.updateTime,
                'updateName': this.dataForm.updateName,
                'version': this.dataForm.version,
                'gcFlag': this.dataForm.gcFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
